#include "drv_i2c_reg.h"
#include <rtdef.h>
#include <rtdevice.h>

#define I2C_BUS_NAME          "i2c2"  /* 传感器连接的I2C总线设备名称 */

static struct rt_i2c_bus_device *i2c_bus = RT_NULL;     /* I2C总线设备句柄 */
static rt_bool_t initialized = RT_FALSE;                /* 传感器初始化状态 */

struct rt_thread i2c_thread;
rt_uint8_t i2c_thread_stack[1024];

static void i2c_init(const char *name)
{
    rt_uint8_t temp[2] = {0, 0};

    /* 查找I2C总线设备，获取I2C总线设备句柄 */
    i2c_bus = (struct rt_i2c_bus_device *)rt_device_find(name);

    if (i2c_bus == RT_NULL)
    {
        rt_kprintf("can't find %s device!\n", name);
    }
}

void i2c_thread_entry(void *parameter)
{
    struct rt_i2c_msg msg;
    rt_uint8_t data[4] = {0x3C, 0x00, 0x00, 0x3C};

    msg.addr = 0xF0;
    msg.flags = RT_I2C_WR;
    msg.buf = data;
    msg.len = 0;
    while (1)
    {
        rt_thread_mdelay(50);

        rt_i2c_transfer(i2c_bus, &msg, 1);
    }
}

void i2c_test(void)
{
    i2c_init(I2C_BUS_NAME);

    rt_thread_init(&i2c_thread, "i2c", i2c_thread_entry, RT_NULL,
                    i2c_thread_stack, 1024, 1, 1);
}
